<?php
session_start();
include "../php/config.php";
if(empty($_SESSION["login.username"]) || empty($_SESSION["cart.id"]))
	die("<meta http-equiv='refresh' content='0;URL=media.php'>");
if($_GET["mdl"] == "checkout")
	$cart_id = $_SESSION["cart.id"];
elseif($_GET["mdl"] == "print")
	$cart_id = $_GET["id"];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
	<title>Media Marketindo - Customer Page</title>
	<link href="../css/checkout.css" rel="stylesheet" type="text/css" />
	<script language="javascript" type="text/javascript">
	var round_decimal = <?php if(!empty($_SESSION["setting.decimal"])) echo $_SESSION["setting.decimal"]; else echo "0";?>,
	round_money = <?php if(!empty($_SESSION["setting.money"])) echo $_SESSION["setting.money"]; else echo "0";?>;
	$(function()
	{
		$("#form_order").submit(function()
		{
			if(confirm("Apakah Anda yakin?"))
				return true;
			return false;
		});
	});
	</script>
</head>
<body<?php if($_GET["mdl"] == "print") echo " onload=\"window.print()\""; ?>>
	<?php
	if($_GET["mdl"] == "checkout")
	{
	?>
	<h2>Cart #<?php echo $cart_id; ?></h2>
	<form action="media.php?m=order&md=cart_post" id="form_checkout" method="post">
	<input name="id" type="hidden" value="<?php echo $cart_id; ?>">
	<table style="border:0px;">
		<tr><td>Customer</td><td><?php echo $_SESSION["login.name"]." (".$_SESSION["login.username"].")"; ?></td></tr>
		<tr><td>Tanggal</td><td><?php echo date("d-m-Y"); ?></td></tr>
	</table>
	<?php
	}
	elseif($_GET["mdl"] == "print")
	{	
		$get_order = mysql_fetch_array(mysql_query("
		SELECT a.id, b.vcKode, b.dtTanggal, b.intJTHari, b.intStatus, b.intApproved, b.vcKeterangan
		FROM web_cart a
		JOIN thorderjual b ON a.order_id = b.intNomor 
		WHERE a.id = $cart_id"));
		$dt = explode(" ",$get_order["dtTanggal"]);
		$d = explode("-",$dt[0]);
	?>
	<h2><?php echo $get_order["vcKode"]." (Cart #".$get_order["id"].")"; ?></h2>
	<table style="border:0px;">
		<tr><td>Customer</td><td><?php echo $_SESSION["login.name"]." (".$_SESSION["login.username"].")"; ?></td></tr>
		<tr><td>Tanggal</td><td><?php echo "$d[2]-$d[1]-$d[0]"; ?></td></tr>
	</table>
	<?php
	}
	?>
	<br />
	<table width="100%">
		<thead>
			<tr>
				<th></th>
				<th colspan="2">Nama</th>
				<th>Jumlah</th>
				<th>Satuan</th>
				<th>Jumlah unit</th>
				<th>Satuan unit</th>
				<th>Harga</th>
				<th>Diskon</th>
				<th>Netto</th>
				<th>Total</th>
			</tr>
		</thead>
		<tbody>
		<?php
		$post = true;
		$get_detail = mysql_query("
		SELECT d.id, d.good_id AS NomorBarang, b.vcNamaBeli AS NamaBarang, 
		d.unit1_id AS NomorSatuanHarga, sh.vcNama AS NamaSatuanHarga, d.unit2_id AS NomorSatuanUnit, su.vcNama AS NamaSatuanUnit
		FROM web_cart_detail d 
		JOIN mbarang b ON d.good_id = b.intNomor
		JOIN msatuan sh ON d.unit1_id = sh.intNomor
		JOIN msatuan su ON d.unit2_id = su.intNomor
		WHERE d.cart_id = $cart_id AND d.status = 1 AND b.intStatus = 1 AND b.intAktif = 1");
		$no = 1;
		$total_akhir = 0;
		while($r = mysql_fetch_array($get_detail))
		{
			$jumlah_unit = 0;
			$html_shade = "";
			$get_shade = mysql_query("SELECT * FROM web_cart_shade WHERE cart_detail_id = ".$r["id"]." AND status = 1");
			$count_shade = mysql_num_rows($get_shade);
			if($count_shade > 0)
			{
				while($shade = mysql_fetch_array($get_shade))
				{
					$jumlah_unit += $shade["quantity"];
					$html_shade .= "
					<tr>
					<td></td>
					<td>".$shade["shade"]."</td>
					<td align=\"right\">".number_format($shade["quantity"],$_SESSION["setting.decimal"])."</td>
					<td colspan=\"8\"></td>
					</tr>";
				}
			}
			$valuta = $_SESSION["setting.valuta"];
			$nomor_barang = $r["NomorBarang"];
			$get_satuan_harga = mysql_fetch_array(mysql_query("
			SELECT intNomor AS NomorSatuan, vcNama AS NamaSatuan, intNomorMSatuan1 AS NomorSatuan1, decKonversi AS NilaiKonversi, decHarga AS PriceList
			FROM (
				SELECT 1 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, 1 AS decKonversi,
					CASE
						WHEN $valuta = 1 THEN a.decHargaPL1
						WHEN $valuta = 2 THEN a.decHargaPL1USD
						WHEN $valuta = 3 THEN a.decHargaPL1RMB
					END AS decHarga
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan1 WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
				UNION ALL
				SELECT 2 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, decKonversi2 AS decKonversi,
					CASE
						WHEN $valuta = 1 THEN a.decHargaPL2
						WHEN $valuta = 2 THEN a.decHargaPL2USD
						WHEN $valuta = 3 THEN a.decHargaPL2RMB
					END AS decHarga
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan2 WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
				UNION ALL
				SELECT 3 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, decKonversi2 * decKonversi3 AS decKonversi,
					CASE 
						WHEN $valuta = 1 THEN a.decHargaPL3
						WHEN $valuta = 2 THEN a.decHargaPL3USD
						WHEN $valuta = 3 THEN a.decHargaPL3RMB
					END AS decHarga
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan3
				WHERE a.intNomor = $nomor_barang AND b.intNomor > 0)
			a WHERE a.intNomor = ".$r["NomorSatuanHarga"]." ORDER BY a.intOrder"));
			$harga = $get_satuan_harga["PriceList"];
			if(empty($harga))
				$harga = 0;
			$get_satuan_unit = mysql_fetch_array(mysql_query("
			SELECT intNomor AS NomorSatuan, vcNama AS NamaSatuan, decKonversi AS Konversi,
				CASE
					WHEN intNomor = 0 THEN 0
					WHEN intNomor = intNomorMSatuan1 THEN 1
					WHEN intNomor = intNomorMSatuan2 THEN decKonversi2
					WHEN intNomor = intNomorMSatuan3 THEN decKonversi2 * decKonversi3
				ELSE 0
				END AS KonversiSatuan
			FROM (
				SELECT 1 AS intOrder, b.intNomor , b.vcNama,
					CASE
						WHEN 0 = 0 THEN 0
						WHEN 0 = intNomorMSatuan1 THEN 1
						WHEN 0 = intNomorMSatuan2 THEN 1/decKonversi2
						WHEN 0 = intNomorMSatuan3 THEN 1/(decKonversi2 * decKonversi3)
					ELSE 0
					END AS decKonversi,
				a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan1
				WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
				UNION ALL
				SELECT 2 AS intOrder, b.intNomor , b.vcNama,
					CASE 
						WHEN 0 = 0 THEN 0
						WHEN 0 = intNomorMSatuan1 THEN decKonversi2
						WHEN 0 = intNomorMSatuan2 THEN 1
						WHEN 0 = intNomorMSatuan3 THEN 1/decKonversi3
					ELSE 0
					END AS decKonversi,
				a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan2
				WHERE a.intNomor = $nomor_barang AND b.intNomor > 0 AND a.decKonversi2 > 0
				UNION ALL
				SELECT 3 AS intOrder, b.intNomor , b.vcNama,
					CASE
						WHEN 0 = 0 THEN 0
						WHEN 0 = intNomorMSatuan1 THEN (decKonversi2 * decKonversi3)
						WHEN 0 = intNomorMSatuan2 THEN decKonversi3
						WHEN 0 = intNomorMSatuan3 THEN 1
					ELSE 0
					END AS decKonversi,
				a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan3
				WHERE a.intNomor = $nomor_barang AND b.intNomor > 0 AND a.decKonversi3 > 0)
			a WHERE a.intNomor = ".$r["NomorSatuanUnit"]." ORDER BY a.intOrder"));
			$konversi_unit = $get_satuan_unit["KonversiSatuan"];
			if(empty($konversi_unit))
				$konversi_unit = 0;
			$get_diskon = mysql_fetch_array(mysql_query("SELECT discount_rate AS DiskonBrand FROM web_discount WHERE customer_id = ".$_SESSION["login.id"]." AND brand_id = ".$r["NomorBrand"]." AND status = 1"));
			$diskon = $get_diskon["DiskonBrand"];
			if(empty($diskon))
				$diskon = 0;
			$jumlah_harga = $jumlah_unit * $konversi_unit;
			$netto = $harga - ($harga * $diskon / 100);
			$total = $jumlah_harga * $netto;
			$total_akhir += $total;
			if($jumlah_harga <= 0)
				$post = false;
			echo"
			<tr>
			<td align=\"center\">$no.</td>
			<td colspan=\"2\">".$r["NamaBarang"]."</td>
			<td align=\"right\">".number_format($jumlah_harga,$_SESSION["setting.decimal"],",",".")."</td>
			<td align=\"center\">".$r["NamaSatuanHarga"]."</td>
			<td align=\"right\">".number_format($jumlah_unit,$_SESSION["setting.decimal"],",",".")."</td>
			<td align=\"center\">".$r["NamaSatuanUnit"]."</td>
			<td align=\"right\">".number_format($harga,$_SESSION["setting.money"],",",".")."</td>
			<td align=\"right\">$diskon%</td>
			<td align=\"right\">".number_format($netto,$_SESSION["setting.money"],",",".")."</td>
			<td align=\"right\">".number_format($total,$_SESSION["setting.money"],",",".")."</td>
			</tr>
			$html_shade";
			$no++;
		}
		?>	
		</tbody>
		<tfoot>
			<tr>
				<td align="right" colspan="10"><b>TOTAL</b></td>
				<td align="right"><?php echo number_format($total_akhir,$_SESSION["setting.money"],",","."); ?></td>
			</tr>
			<tr>
				<td colspan="11">Keterangan : <br />
					<?php
					if($_GET["mdl"] == "checkout")
						echo "<textarea name=\"keterangan\" rows=\"7\" style=\"width:99.5%;\"></textarea>";
					elseif($_GET["mdl"] == "print")
						echo $get_order["vcKeterangan"];
					?>
				</td>
			</tr>
		</tfoot>
	</table>
	<?php if($_GET["mdl"] == "checkout") { ?>	
	<p align="right">
		<input type="button" onclick="document.location.href = 'media.php';" value="Back">
		<?php
		if($post)
			echo "<input type=\"submit\" value=\"ORDER\">";
		else
			echo "<input type=\"button\" value=\"ORDER\" onClick=\"alert('Cart belum valid, silahkan Anda melengkapi jumlah barangnya hingga valid terlebih dahulu')\">";
		?>
	</p>
	</form>
	<?php } ?>
</body>
</html>